<!--
**********************************************************
* @Author: 张小平
* @Date:   2024-05-01
* @Email:  qqshuqian@163.com
* @Last modified: 最后修改日期
* @Description: 文件描述
**********************************************************
-->
<template>
  <div class="wrap" v-if="tree && tree.type">
    <template v-if="isStartNode">
      <NodeBox isStart>
        <Item :name="nodeName"
          @on-set="handleSetItem"
          @on-del="handleDelItem"
          :item="tree"
          :isActive="tree.id === activeId">发起人</Item>
        <Btns @on-add-item="((type) => handleAddItem(tree, type))" ></Btns>
      </NodeBox>
    </template>
    <template v-else-if="tree.type !== nodeType.ROUTE">
      <NodeBox>
        <Item :name="nodeName"
          @on-set="handleSetItem"
          @on-del="handleDelItem"
          :item="tree"
          :isActive="isActive">
          {{procStartUser}}
          <template v-for="user in assignMember">
          {{user.name}}&nbsp;
          </template>
          {{ roleInfo.name }}
          {{ fieldContact.label }}
        </Item>
        <Btns @on-add-item="((type) => handleAddItem(tree, type))" />
      </NodeBox>
    </template>
    <template v-if="isBranch">
      <BranchBox>
        <AddBranch @click="handleAddBranch" />
        <BranchCol :isStart="index === 0" :isEnd="index === (tree.conditions.length - 1)" v-for="(branch, index) in tree.conditions" :key="index">
          <BoxCondition>
            <ItemCondition
            @on-set="handleSetItem"
            @on-del="handleDelBranch"
            :item="branch"
            :isActive="branch.id === activeId"
            :index="index">{{renderBranch(branch.properties)}}</ItemCondition>
            <Btns @on-add-item="((type) => handleAddItem(branch, type))" />
          </BoxCondition>
          <template v-if="branch.child">
            <BoxItem
              :tree="branch.child"
              :onActive="onActive"
              :onDelete="onDelete"
              :onDeleteBranch="onDeleteBranch"
              :onAdd="onAdd"
              :onAddBranch="onAddBranch"
              :isActive="branch.child.id === activeId"
              :activeId="activeId" />
          </template>
        </BranchCol>
      </BranchBox>
      <NodeBox isStart>
        <Btns @on-add-item="((type) => handleAddItem(tree, type))" />
      </NodeBox>
    </template>
    <template v-if="!!this.tree.child">
      <BoxItem
        :tree="tree.child"
        :onActive="onActive"
        :onDelete="onDelete"
        :onDeleteBranch="onDeleteBranch"
        :onAdd="onAdd"
        :onAddBranch="onAddBranch"
        :isActive="tree.child.id === activeId"
        :activeId="activeId" />
    </template>
  </div>
</template>

<script>
import { Btns, NodeBox, Item } from './node'
import { BranchBox, BranchCol, BoxCondition, ItemCondition, AddBranch } from './branch'
import { nodeType, itemType, assignType, branchType } from '@/framework-code/utils/constants'
export default {
  name: 'BoxItem',
  props: {
    tree: Object,
    activeId: {
      type: String,
      default: ''
    },
    isActive: {
      type: Boolean,
      default: false
    },
    onActive: Function,
    onDelete: Function,
    onAdd: Function,
    onDeleteBranch: Function,
    onAddBranch: Function
  },
  components: {
    Btns,
    NodeBox,
    Item,
    BranchBox,
    BranchCol,
    BoxCondition,
    ItemCondition,
    AddBranch
  },
  data() {
    return {
      nodeType
    }
  },
  computed: {
    isBranch() {
      const {
        conditions
      } = this.tree
      if (conditions && conditions.length > 1) {
        return true
      } else {
        return false
      }
    },
    isStartNode() {
      if (this.tree.type === nodeType.STARTTASK) {
        return true
      } else {
        return false
      }
    },
    nodeName() {
      const {
        type
      } = this.tree
      switch (type) {
        case nodeType.USERTASK:
          return itemType.todo
        case nodeType.CCTASK:
          return itemType.cc
        default:
          return itemType.start
      }
    },
    procStartUser() {
      const {
        type,
        properties
      } = this.tree
      if (type === nodeType.USERTASK || type === nodeType.CCTASK) {
        if (properties?.personList) {
          const {
            personType
          } = properties.personList[0]
          if (personType === assignType.t3) {
            return '发起人'
          }
        }
      }
    },
    assignMember() {
      const {
        type,
        properties
      } = this.tree
      if (type === nodeType.USERTASK || type === nodeType.CCTASK) {
        if (properties?.personList) {
          const {
            personType,
            assignMember
          } = properties.personList[0]
          if (personType === assignType.t1 && assignMember) {
            return JSON.parse(assignMember)
          }
        }
      }
      return []
    },
    roleInfo() {
      const {
        type,
        properties
      } = this.tree
      if (type === nodeType.USERTASK || type === nodeType.CCTASK) {
        if (properties?.personList) {
          const {
            personType,
            roleInfo
          } = properties.personList[0]
          //JSON.stringify("333333"+properties.personList[0])
          if (personType === assignType.t4 && roleInfo) {
            if (typeof roleInfo === 'string') {
              return JSON.parse(roleInfo)
            }
            return roleInfo
          }
        }
      }
      return {
        name: ''
      }
    },
    fieldContact() {
      const {
        type,
        properties
      } = this.tree
      if (type === nodeType.USERTASK || type === nodeType.CCTASK) {
        if (properties?.personList) {
          const {
            personType,
            fieldContact
          } = properties.personList[0]
          if (personType === assignType.t2 && fieldContact) {
            return JSON.parse(fieldContact)
          }
        }
      }
      return {
        label: ''
      }
    }
  },
  methods: {
    handleAddBranch() {
      this.onAddBranch(this.tree)
    },
    handleSetItem(e, item) {
      this.onActive(item)
    },
    handleAddItem(item, type) {
      debugger
      this.onAdd(item, type, 'add')
    },
    handleDelItem(e, item) {
      this.onDelete(item)
    },
    handleDelBranch(e, item, index) {
      this.onDeleteBranch(item, index)
    },
    renderBranch(properties) {
      if (properties?.branchConditionList) {
        const { branchConditionList } = properties
        let str = ''
        branchConditionList.map((condition) => {
          const {
            conditionValue,
            numberCondition,
            type,
            fieldName
          } = condition

          const conditionArr = JSON.parse(conditionValue)
          switch (type) {
            case branchType.org: {
              let str2 = ''
              conditionArr.map((item, i) => {
                if (i > 0) {
                  str2 += `，${item.name}`
                } else {
                  str2 += item.name
                }
              })
              if (str2) {
                if (str) {
                  str += ' 并且  '
                }
                str += `机构有：${str2}！`
              }
              break
            }
            case branchType.role: {
              let str2 = ''
              conditionArr.map((item, i) => {
                if (i > 0) {
                  str2 += `，${item.name}`
                } else {
                  str2 += item.name
                }
              })
              if (str2) {
                if (str) {
                  str += ' 并且  '
                }
                str += `角色有：${str2}！`
              }
              break
            }
            case branchType.startUser: {
              let str2 = ''
              conditionArr.map((item, i) => {
                if (i > 0) {
                  str2 += `，${item.name}`
                } else {
                  str2 += item.name
                }
              })
              if (str2) {
                if (str) {
                  str += ' 并且  '
                }
                str += `发起人有：${str2}！`
              }
              break
            }
            case branchType.number: {
              let str2 = ''
              conditionArr.map((item, i) => {
                if (i > 0) {
                  str2 += `,${item}`
                } else {
                  str2 += `${item}`
                }
              })
              if (str2) {
                if (str) {
                  str += ' 并且  '
                }
                str += `${fieldName}：${numberCondition} ${str2} !`
              }
              break
            }
            default: {
              let str2 = ''
              conditionArr.map(item => {
                str2 += item.label
              })

              if (str2) {
                if (str) {
                  str += ' 并且  '
                }
                str += `${fieldName}条件为：${str2}！`
              }
            }
          }
        })
        if (str) { return str }
      }

      return '请设置条件'
    }
  }
}
</script>
